import com.sun.mail.util.MailSSLSocketFactory;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.security.SecureRandom;
public class Tools {
    public boolean hasNumber(String str){
        for(char ch: str.toCharArray()) {
            if (Character.isDigit(ch)) {
                return true;
            }
        }
            System.out.println("该密码里没有数字");
            return false;
    }
    public boolean hasBigletter(String str){
        for(char ch:str.toCharArray()){
            if(Character.isUpperCase(ch)){
                return true;
            }
        }
        System.out.println("该密码里没有大写字母");
        return false;
    }
    public void sendEmail(String newpassword)throws Exception{
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com");
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
        prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发件人邮件用户名、授权码
                return new PasswordAuthentication("1043693193@qq.com", "vboocjaxfwtwbfic");
            }
        });
        session.setDebug(true);
        Transport ts=session.getTransport();
        ts.connect("smtp.qq.com","1043693193@qq.com","vboocjaxfwtwbfic");
        MimeMessage message=new MimeMessage(session);
        message.setFrom(new InternetAddress("1043693193@qq.com"));
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("1043693193@qq.com"));
        message.setSubject("这是重新为您生成的随机密码");
        SecureRandom random=new SecureRandom();
        int randomNumber=random.nextInt(10000000);
        String randomNumberString = String.valueOf(randomNumber);
        message.setContent(newpassword,"text/html;charset=UTF-8");
        ts.sendMessage(message,message.getAllRecipients());
        ts.close();
    }
}
